//
//  EnterShareViewController.m
//  SXH
//
//  Created by 龚啸 on 16/2/29.
//  Copyright © 2016年 lyss. All rights reserved.
//

#import "EnterShareViewController.h"
#import "SharMapViewController.h"
#import "LoginViewController.h"
#import "UserEntity.h"
#import "CommonService.h"
#import "LocationManagement.h"
#import "SVProgressHUD.h"

@interface EnterShareViewController ()
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@end

@implementation EnterShareViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _titleLabel.text = [NSString stringWithFormat:@"房间：%@",_room_id];
    // Do any additional setup after loading the view from its nib.
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

- (IBAction)doBack:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}
- (IBAction)enterRoom:(id)sender {
     UserEntity *userEntity = [UserEntity sharedInstance];
    
    if (userEntity.login == NO) {
        LoginViewController *vc = [[LoginViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
        return;
    }
    
    LocationManagement *lm = [LocationManagement sharedInstance];
    float _currentLontitude = lm.location.coordinate.longitude;
    float _currentLatitude =  lm.location.coordinate.latitude;
    CommonService *service = [[CommonService alloc] init];
    [SVProgressHUD show];
    if (_currentLontitude!=0 && _currentLatitude != 0  ) {
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:userEntity.userId,@"user_id",[NSString stringWithFormat:@"%f",_currentLontitude],@"latitude",[NSString stringWithFormat:@"%f",_currentLatitude],@"longitude",_room_id,@"room_id", nil];
        [service getNetWorkData:dic Path:JOIN_GROUP Successed:^(id entity) {
            NSDictionary *dic = (NSDictionary *)entity;
            NSInteger  code = [[dic objectForKey:@"code"] integerValue];
            if (code != 0) {
                [SVProgressHUD showErrorWithStatus:@"口令过期，房间不存在"];
            }else{
                [SVProgressHUD dismiss];
               
                SharMapViewController *vc = [[SharMapViewController alloc] init];
                vc.userId = userEntity.userId;
                CLLocation * userLocation = [[CLLocation alloc]init];
                userLocation = [[CLLocation  alloc] initWithLatitude:_currentLatitude longitude:_currentLontitude];
                vc.userLocation = userLocation;
                vc.room_id = _room_id;
                [self.navigationController pushViewController:vc animated:YES];
            }
        } Failed:^(NSInteger errorCode, NSString *message) {
           [SVProgressHUD showErrorWithStatus:@"网络断了"];
        }];
        

    }else{
        [SVProgressHUD showErrorWithStatus:LocationOpenTip];
    }
   
}

@end
